var api = require('../config/api.js')
var oneNetSDK = require('onenetstudio-api-node-sdk')
const config = {
  author_key: api.author_key,//自己账号的key
  user_id: api.user_id,
  version: '2020-05-29'
  //url: ''可自定义url,不需要配置的时候，删除url这个字段即可，默认：https://openapi.heclouds.com
}
oneNetSDK = new oneNetSDK(config);//实例化之后便可以调用oneNetSDK

// APPQueryDeviceProperty();



/**********以下代码为批量读取Onenet studio平台生成的设备，并存储在deviceid的表中************** */
// var device_offset = 10;
// var adddevicedata = 0;

// const myintervalid = setInterval(function () {
//     if((adddevicedata == 0)&&(device_offset < 1001)){
//         adddevicedata = 1;
//         //项目集成设备列表
//         const params = {
//             query:{
//                 "project_id": api.project_id,
//                 "product_id": api.product_id,
//                 "from":'1',
//                 "offset":device_offset,
//                 "limit":'1'


//             },
//             body:{}
//         }
//         const data = oneNetSDK.APPQueryDeviceList(params).then(function (res) {

//             // console.log('wxtoonenet updata123232', res);
//             // console.log('wxtoonenet updata123232', res.data.list.length);

//             wx.cloud.callFunction({
//                 name: 'adddeviceList',
//                 data: { 
//                     "deviceListdata":res.data.list[0]
//                 }
//                 }).then(res => {
//                     console.log("cloud function delete device ", res);
//                     adddevicedata = 0;
//                     if(res.errMsg == "cloud.callFunction:ok"){
//                         device_offset = device_offset + 1;
//                     }
//                 });

//         });
//         console.log("adddevicedata", device_offset);

//     }
    

// }, 2000);
/************************* */
/*
* 添加设备 读取设备信息 添加到设备数据库表中
* 设备数量    起始地址
*/
function adddeviceList(offset) {

    //项目集成设备列表
    const params = {
        query:{
            "project_id": api.project_id,
            "product_id": api.product_id,
            "from":'1',
            "offset":offset,
            "limit":'1'


        },
        body:{}
    }
    const data = oneNetSDK.APPQueryDeviceList(params).then(function (res) {

        console.log('wxtoonenet updata123232', res);
        console.log('wxtoonenet updata123232', res.data.list.length);

        wx.cloud.callFunction({
            name: 'adddeviceList',
            data: { 
                "deviceListdata":res.data.list[0]
            }
            }).then(res => {
                console.log("cloud function delete device ", res);
                return res.errMsg;
            });

    });
}



//项目集成产品列表
// const params = {
//   query:{
//     project_id: api.project_id
//   },
//   body:{}
//   // body:{"msg":"559932020134955848AA"}
// }
// const data = oneNetSDK.APPQueryProductList(params).then(function (res) {

//     console.log('wxtoonenet updata123232', res)

// });
// APPQueryDeviceStatus(params);

/*
* GET方法
* 给onenet studio平台发送数据，设备属性最新数据查询-----APPQueryDeviceProperty
* number mydevice_id, params
* @returns HTTP api返回值   data.status:int 类型 设备状态 1-未激活 2-在线 3-离线
*/
function APPQueryDeviceProperty() {
    const params2 = {
        query:{
            "project_id": api.project_id,
            "product_id": api.product_id,
            "device_name": "mycarep1",
        },
        "body": {
            "params": {}
        }
    }
    const data2 = oneNetSDK.APPQueryDeviceProperty(params2).then(function (res) {

        console.log('APPQueryDeviceProperty111', res);
        return(res);

    });
};

/*
* GET方法
* 给onenet studio平台发送数据，设备状态查询-----APPQueryDeviceStatus
* number mydevice_id, params
* @returns HTTP api返回值   data.status:int 类型 设备状态 1-未激活 2-在线 3-离线
*/
function APPQueryDeviceStatus(params) {
    const params2 = {
        query:{
            "project_id": api.project_id,
            "product_id": api.product_id,
            "device_name": "mycarep1",
        },
        "body": {
            "params": {}
        }
    }
    const data2 = oneNetSDK.APPQueryDeviceStatus(params2).then(function (res) {

        console.log('APPQueryDeviceStatus', res);
        return(res);

    });
};





/*
* POST方法
* 给onenet studio平台发送数据，设备属性设置-----APPSetDeviceProperty
* number mydevice_id, params
* @returns HTTP api返回值
*/
function APPSetDeviceProperty(params) {
  const params2 = {
    query:{},
    "body": {
      "project_id": api.project_id,
      "product_id": api.product_id,
      "device_name": "mycarep1",
      "params": params
    }
  }
  const data2 = oneNetSDK.APPSetDeviceProperty(params2).then(function (res) {

      console.log('wxtoonenet APPSetDeviceProperty', res);
      return(res);

  });
};

module.exports = {
    oneNetSDK,
    APPQueryDeviceStatus,
    APPSetDeviceProperty

};